# -*- coding: utf-8 -*-
# @Time : 2023/3/14
# @Author : 限量老

from django import forms
from rest_framework import serializers

from .models import Course
from django.contrib.auth.models import User


class CourseForm(forms.ModelForm):
    class Meta:
        model = Course
        fields = ("name", 'introduction', 'teacher', 'price')


class UserSerializer(serializers.ModelSerializer):  # 序列化用户模型类
    class Meta:
        model = User
        fields = '__all__'


class CourseSerializer(serializers.ModelSerializer):  #serializers模型类写法与上面的CourseForm类似
    teacher=serializers.ReadOnlyField(source='teacher.username') #外键字段 ,只读
    class Meta:
        model=Course
        # exclude=("id",)   #排除,只有一个要加 ,
        # fields=("name",'introduction','teacher','price')
        fields='__all__'  #如果是全部
        depth=2 #外键关联遍历的深度

# class CourseSerializer(serializers.HyperlinkedModelSerializer):  # 带超链接的序列化器
#     teacher = serializers.ReadOnlyField(source='teacher.username')  # 外键字段 ,只读
#
#     class Meta:
#         model = Course
#         # url是默认值,可在settings.py中设置 URL_FIELD_NAME使全局生效
#         fields = ('id', "name", 'url', 'introduction', 'teacher', 'price', 'created_at', 'updated_at')  # 多了url



























